package com.sky.common.task;

import com.sky.business.sys.domain.CameraConfig;
import com.sky.business.sys.service.CameraConfigService;
import com.sky.common.track.DeviceStatus;
import com.sky.common.track.TrackTaskManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 同步心跳数据到数据库
 *
 */
@Component
public class CameraHeartbeatSyncTask {

    @Autowired
    private CameraConfigService cameraConfigService;

    @Scheduled(cron = "*/5 * * * * ?")
    public void doTask() {
        List<CameraConfig> cameraConfigList = cameraConfigService.list();
        for (CameraConfig cameraConfig : cameraConfigList) {
            DeviceStatus deviceStatus = TrackTaskManager.getDeviceStatus(cameraConfig.getCameraDeviceId());
            if (deviceStatus.getLastHeartbeat() != null && (cameraConfig.getLastHeartBeatTime() == null || deviceStatus.getLastHeartbeat().after(cameraConfig.getLastHeartBeatTime()))) {
                //更新数据库
                cameraConfigService.updateHeartbeat(cameraConfig.getId(), deviceStatus.getLastHeartbeat());
            }

        }
    }
}
